/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.layer.service.impl;

import com.jfinal.kit.StrKit;
import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.core.aop.AopKit;
import com.lambkit.db.sql.column.Example;
import com.mapfinal.render.style.SimpleFillSymbol;
import com.mapfinal.render.style.SimpleLineSymbol;
import com.mapfinal.render.style.SimpleMarkerSymbol;
import com.mapfinal.server.layer.MfStyleService;
import com.mapfinal.server.layer.model.MfStyle;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2022-04-02
 * @version 1.0
 * @since 1.0
 */
public class MfStyleServiceImpl extends LambkitModelServiceImpl<MfStyle> implements MfStyleService {
	
	private MfStyle DAO = null;
	
	public MfStyle dao() {
		if(DAO==null) {
			DAO = AopKit.singleton(MfStyle.class);
		}
		return DAO;
	}

	@Override
	public MfStyle getStyle(String name) {
		// TODO Auto-generated method stub
		if(StrKit.isBlank(name)) return null;
		String ln = null,wn = null;
		if(name.contains(":")) {
			String[] names = name.split(":");
			ln = names[1];
			wn = names[0];
		}
		if(StrKit.isBlank(ln)) {
			return null;
		}
		System.out.println("workspace=" + wn + ", style=" + ln);
		if(StrKit.isBlank(wn)) {
			Example example = MfStyle.sql().andNameEqualTo(ln).andWorkspaceIsNull().example();
			return findFirst(example);
		} else {
			Example example = MfStyle.sql().andNameEqualTo(ln).andWorkspaceEqualTo(wn).example();
			return findFirst(example);
		}
	}

	@Override
	public MfStyle getDefault(String geotype) {
		// TODO Auto-generated method stub
		String geotypeStr = geotype.toLowerCase();
		if(geotypeStr.contains("point")) {
			return getDefaultPoint();
		} else if(geotypeStr.contains("line")) {
			return getDefaultLine();
		} else if(geotypeStr.contains("polygon")) {
			return getDefaultPolygon();
		} else if(geotypeStr.contains("raster")) {
			return getDefaultRaster();
		} 
		return null;
	}

	@Override
	public MfStyle getDefaultPoint() {
		// TODO Auto-generated method stub
		String name = "point";
		Example example = MfStyle.sql().andNameEqualTo(name).andWorkspaceIsNull().example();
		MfStyle style = findFirst(example);
		if(style==null) {
			style = new MfStyle();
			style.setName(name);
			style.setContent("");
			style.setType(0);
			style.setGeotype(name);
			style.setSymbol(SimpleMarkerSymbol.DEFAULT());
		}
		return style;
	}

	@Override
	public MfStyle getDefaultLine() {
		// TODO Auto-generated method stub
		String name = "line";
		Example example = MfStyle.sql().andNameEqualTo(name).andWorkspaceIsNull().example();
		MfStyle style = findFirst(example);
		if(style==null) {
			style = new MfStyle();
			style.setName(name);
			style.setContent("");
			style.setType(0);
			style.setGeotype(name);
			style.setSymbol(SimpleLineSymbol.DEFAULT());
		}
		return style;
	}

	@Override
	public MfStyle getDefaultPolygon() {
		// TODO Auto-generated method stub
		String name = "polygon";
		Example example = MfStyle.sql().andNameEqualTo(name).andWorkspaceIsNull().example();
		MfStyle style = findFirst(example);
		if(style==null) {
			style = new MfStyle();
			style.setName(name);
			style.setContent("");
			style.setType(0);
			style.setGeotype(name);
			style.setSymbol(SimpleFillSymbol.DEFAULT());
		}
		return style;
	}

	@Override
	public MfStyle getDefaultRaster() {
		// TODO Auto-generated method stub
		String name = "raster";
		Example example = MfStyle.sql().andNameEqualTo(name).andWorkspaceIsNull().example();
		MfStyle style = findFirst(example);
		if(style==null) {
			style = new MfStyle();
			style.setName(name);
			style.setContent("");
			style.setType(0);
			style.setGeotype(name);
		}
		return style;
	}
}
